package com.woniuxy.article.controller;

import com.hc.entity.Comment;
import com.hc.utils.ResponseEntity;
import com.hc.vo.AgreeVo;
import com.woniuxy.article.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description：
 * @Author：曹杰克
 * @CreateTime：2022/10/20 15:50
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;
    @RequestMapping("/add")
    public ResponseEntity addComment(@RequestBody Comment commnet){
        commentService.addComment(commnet);
        return ResponseEntity.SUCCESS;

    }
    @PostMapping("/addcoment")
    public ResponseEntity  updateAgreenum(@RequestBody Comment commnet){
        commentService.addComment(commnet);
        return ResponseEntity.SUCCESS;

    }
    @PostMapping("/disagree")
    public ResponseEntity updatedisAgreenum( @RequestBody Comment commnet){
        commentService.updatedisAgreenum(commnet);
        return ResponseEntity.SUCCESS;

    }
    @PostMapping("/settop")
    public ResponseEntity  setTop(@RequestBody Comment commnet){
        commentService.setTop(commnet);
        return ResponseEntity.SUCCESS;
    }
    //设置评论为非置顶
    @PostMapping("/downtop")
    public ResponseEntity  downtop(@RequestBody Comment commnet){
        commentService.downtop(commnet);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping("/getTopComment")
    public ResponseEntity  getTopComment(@RequestBody Comment commnet){
        Comment topComment = commentService.getTopComment(commnet);
        return new ResponseEntity("200","ok",topComment);
    }


    @PostMapping("/comments")
    public ResponseEntity  getByPaperId(@RequestBody Comment commnet){
        List<Comment> allCommnets = commentService.getByPaperId(commnet);
        //将属于某个子菜单的子菜单加入该菜单的SubMenus中
        List<Comment> allCommnets1=new ArrayList<>();
        for (Comment comment: allCommnets) {
            if(comment.getFid() == null){ //顶级菜单
                allCommnets1.add(comment);
            }
            List<Comment> sonComs= new ArrayList<>();

            for (Comment subAu: allCommnets) {
                if(comment.getId().equals(subAu.getFid())){
                    sonComs.add(subAu);
                }
            }
            comment.setSubComments(sonComs);
        }
        return new ResponseEntity("200","ok",allCommnets1);
    }

    //评论点赞操作：未赞增加 已赞取消
    @PostMapping("/agree")
    public ResponseEntity  agree(@RequestBody AgreeVo agreeVo){
        boolean agree = commentService.agree(agreeVo);
        return new ResponseEntity("200","ok",agree);
    }
}
